<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Socket客户端</title>
</head>
<script type="text/javascript">
    var socket;
    if (window.WebSocket) {
        socket = new WebSocket("ws://localhost:8888/netty");
        socket.onmessage = function (event) {
            var ta = document.getElementById('responseText');
            ta.value = ta.value + "\n" + event.data;
        }
        socket.onopen = function (event) {
            var ta = document.getElementById('responseText');
            ta.value = '连接开启';
        }
        socket.onclose = function (event) {
            var ta = document.getElementById('responseText');
            ta.value = ta.value + "\n" + "连接断开";
        }
    } else {
        alert("浏览器不支持")
    }

    function send(message) {
        if (!window.WebSocket) {
            return;
        }
        if (socket.readyState == WebSocket.OPEN) {
            socket.send(message);
        } else {
            alert("断开连接");
        }
    }

</script>
<body>
<form onsubmit="return false">
    <label>
        <textarea name="message" style="width: 400px;height:200px"></textarea>
    </label>
    <input type="button" value="发送数据" onclick="send(this.form.message.value)"/>
    <p>服务端输出:</p>
    <label>
        <textarea id="responseText" style="width: 400px;height:200px"></textarea>
    </label>
    <input onclick="javascript: document.getElementById('responseText').value =''" type="button" value="清空内容"/>
</form>
</body>
</html>